package com.bbzn.device.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.dto.api.ResultResponse;
import com.bbzn.device.client.dto.api.geocode.GetCodeRequest;
import com.bbzn.device.client.dto.api.geocode.GetCodeResult;
import com.bbzn.device.client.dto.api.position.GetPositionRequest;
import com.bbzn.device.client.dto.api.position.GetPositionResult;
import com.bbzn.device.client.dto.api.weatherInfo.GetWeatherInfoRequest;
import com.bbzn.device.client.dto.api.weatherInfo.GetWeatherInfoResult;
import com.bbzn.device.client.service.GaodeMapApiService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.BeanVOUtils;
import com.bbzn.device.client.utils.HttpClientUtil;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.concurrent.Executor;

import static com.bbzn.device.client.constant.GaodeApiConstant.*;

@ToString
@Slf4j
@Service
public class GaodeMapApiServiceImpl implements GaodeMapApiService {

    @Autowired
    public SystemParamByCacheService systemParamByCacheService;

    @Autowired
    @Qualifier("threadPoolExecutorForVideo")
    private Executor executor;

    public HashMap<String, String> params = new HashMap<>();

    public String url;

    public String key;

    @PostConstruct
    public void init()  {
        try {
            key = systemParamByCacheService.getValue(GAODEMAPAPI, APIKEY).getValue();
        }catch (Exception e){
            log.error(e.getMessage());
        }
    }

    /**
     * 定位服务
     * 根据手机客户端上传终端的基站/wifi 等信息，定位服务返回终端的位置信息。
     *
     * @return
     * @throws Exception
     */
    @Override
    public ResultResponse<GetPositionResult> getPosition(GetPositionRequest request) throws Exception {
        url = systemParamByCacheService.getValue(GAODEMAPAPI, APIURL).getValue();
        if (StringUtils.isEmpty(url) || StringUtils.isEmpty(key) ) {
            throw new RuntimeException("基本參數為空");
        }
        String result = "";
        request.setKey(key);
        params.putAll(BeanVOUtils.objectToMap(request));
        log.info("请求原始报文：" + JSON.toJSONString(params));
        result = HttpClientUtil.doGet(url, params);
        System.out.println(result);
        if (StringUtils.isEmpty(result)) {
            throw new RuntimeException("接口返回參數為空");
        }
        log.info("返回原始报文：" + JSON.toJSONString(result));
        GetPositionResult getPositionResult = JSONObject.parseObject(result, GetPositionResult.class);
        if (null == getPositionResult) {
            throw new RuntimeException("接口返回參數為空");
        }
        return ResultResponse.setSuccess(getPositionResult);
    }

    /**
     * 高德逆地理编码
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public ResultResponse<GetCodeResult> getCode(GetCodeRequest request) throws Exception {
        url = systemParamByCacheService.getValue(GAODEMAPAPI, APICODEURL).getValue();
        if (StringUtils.isEmpty(url) || StringUtils.isEmpty(key) ) {
            throw new RuntimeException("基本參數為空");
        }
        String result = "";
        request.setKey(key);
        params.putAll(BeanVOUtils.objectToMap(request));
        log.info("请求原始报文：" + JSON.toJSONString(params));
        result = HttpClientUtil.doGet(url, params);
        System.out.println(result);
        if (StringUtils.isEmpty(result)) {
            throw new RuntimeException("接口返回參數為空");
        }
        log.info("返回原始报文：" + JSON.toJSONString(result));
        GetCodeResult getCodeRequest = JSONObject.parseObject(result, GetCodeResult.class);
        if (null == getCodeRequest) {
            throw new RuntimeException("接口返回參數為空");
        }
        return ResultResponse.setSuccess(getCodeRequest);
    }

    @Override
    public ResultResponse<GetWeatherInfoResult> getWeatherInfo(GetWeatherInfoRequest request) throws Exception {
        url = systemParamByCacheService.getValue(GAODEMAPAPI, APIWEATHERURL).getValue();
        if (StringUtils.isEmpty(url) || StringUtils.isEmpty(key) ) {
            throw new RuntimeException("基本參數為空");
        }
        String result = "";
        request.setKey(key);
        params.putAll(BeanVOUtils.objectToMap(request));
        log.info("请求原始报文：" + JSON.toJSONString(params));
        result = HttpClientUtil.doGet(url, params);
        System.out.println(result);
        if (StringUtils.isEmpty(result)) {
            throw new RuntimeException("接口返回參數為空");
        }
        log.info("返回原始报文：" + JSON.toJSONString(result));
        GetWeatherInfoResult getCodeRequest = JSONObject.parseObject(result, GetWeatherInfoResult.class);
        if (null == getCodeRequest) {
            throw new RuntimeException("接口返回參數為空");
        }
        return ResultResponse.setSuccess(getCodeRequest);
    }


}
